﻿using System;
using System.Data;
using System.Collections.Generic;

namespace LDSP.BRL
{
    /// <summary>
    /// 企业适用文档关系
    /// </summary>
    public partial class R_EnterpriseCodification : BYFW.BOL.BaseEntity
    {
        public R_EnterpriseCodification()
        {
            m_tableName = "t_r_enterprise_codification";
            m_hasName = true;
            m_hasComment = true;
            m_tblCacheManager.IsEnable = true;
        }

        protected int m_enterpriseID = 0;
        protected int m_ownerID = 0;
        protected int m_docID = 0;
        protected DateTime m_favoriteDT = DateTime.Parse("1970-01-01");
        protected Priority m_priority = Priority.中;
        protected string m_principal = string.Empty;
        protected string m_action = string.Empty;
        protected string m_keyItems = string.Empty;
        protected string m_selfEvaluation = string.Empty;
        protected string m_existingGap = string.Empty;
        protected DateTime m_planFinishDate = DateTime.MaxValue;
        protected DateTime m_actualFinishDate = DateTime.MaxValue;
        protected FavoriteCodificationStatus m_status = FavoriteCodificationStatus.其它;

        protected Enterprise m_enterprise = null;
        protected Codification m_codification = null;

        /// <summary>
        /// 企业ID
        /// </summary>
        public int EnterpriseID
        {
            get { return m_enterpriseID; }
            set { m_enterpriseID = value; }
        }
        /// <summary>
        /// 添加者ID
        /// 这里可以是本公司的用户，也可以是集团公司管理员
        /// </summary>
        public int OwnerID
        {
            get { return m_ownerID; }
            set { m_ownerID = value; }
        }
        /// <summary>
        /// 文档ID
        /// </summary>
        public int DocID
        {
            get { return m_docID; }
            set { m_docID = value; }
        }
        /// <summary>
        /// 收藏时间
        /// </summary>
        public DateTime FavoriteDT
        {
            get { return m_favoriteDT; }
            set { m_favoriteDT = value; }
        }
        /// <summary>
        /// 文档的优先级
        /// </summary>
        public Priority Priority
        {
            get { return m_priority; }
            set { m_priority = value; }
        }
        /// <summary>
        /// 责任人
        /// </summary>
        public string Principal
        {
            get { return m_principal.Trim().Trim(';'); }
            set { m_principal = ";" + value + ";"; }
        }
        /// <summary>
        /// 实施措施
        /// </summary>
        public string Action
        {
            get { return m_action; }
            set { m_action = value; }
        }
        /// <summary>
        /// 关键条款
        /// </summary>
        public string KeyItems
        {
            get { return m_keyItems; }
            set { m_keyItems = value; }
        }
        /// <summary>
        /// 自我评估
        /// </summary>
        public string SelfEvaluation
        {
            get { return m_selfEvaluation; }
            set { m_selfEvaluation = value; }
        }

        /// <summary>
        /// 不符合内容
        /// </summary>
        public string ExistingGap
        {
            get { return m_existingGap; }
            set { m_existingGap = value; }
        }

        /// <summary>
        /// 状态
        /// </summary>
        public FavoriteCodificationStatus Status
        {
            get
            {
                return m_status;
            }
            set
            {
                m_status = value;
            }
        }

        /// <summary>
        /// 计划完成时间
        /// </summary>
        public DateTime PlanFinishDT
        {
            get { return m_planFinishDate; }
            set { m_planFinishDate = value; }
        }

        /// <summary>
        /// 实际完成时间
        /// </summary>
        public DateTime ActualFinishDT
        {
            get { return m_actualFinishDate; }
            set { m_actualFinishDate = value; }
        }

        public Enterprise Enterprise
        {
            get
            {
                if (m_enterprise == null)
                {
                    m_enterprise = new Enterprise();
                    if (m_enterpriseID != 0)
                    {
                        m_enterprise.GetProperties(m_enterpriseID);
                    }
                }
                return m_enterprise;
            }
        }
        public Codification Codification
        {
            get
            {
                if (m_codification == null)
                {
                    m_codification = new Codification();
                    if (m_docID != 0)
                    {
                        m_codification.GetProperties(m_docID);
                    }
                }
                return m_codification;
            }
        }


        protected override void FillContainer()
        {
            base.FillContainer();
            m_container.Add("enterpriseID", m_enterpriseID);
            m_container.Add("ownerID", m_ownerID);
            m_container.Add("docID", m_docID);
            m_container.Add("favoriteDT", m_favoriteDT);
            m_container.Add("priority", (int)m_priority);
            m_container.Add("principal", m_principal);
            m_container.Add("action", m_action);
            m_container.Add("keyItems", m_keyItems);
            m_container.Add("selfEvaluation", m_selfEvaluation);
            m_container.Add("planFinishDate", m_planFinishDate);
            m_container.Add("actualFinishDate", m_actualFinishDate);
            m_container.Add("status", (int)m_status);
            m_container.Add("existingGap", m_existingGap);
        }

        public override bool GetProperties(System.Data.DataRow record)
        {
            if (!base.GetProperties(record))
            {
                return false;
            }
            m_enterpriseID = this.GetIntValue(record, "enterpriseID");
            m_ownerID = this.GetIntValue(record, "ownerID");
            m_docID = this.GetIntValue(record, "docID");
            m_favoriteDT = this.GetDateValue(record, "favoriteDT");
            m_priority = (Priority)this.GetIntValue(record, "priority");
            m_principal = this.GetStringValue(record, "principal");
            m_action = this.GetStringValue(record, "action");
            m_keyItems = this.GetStringValue(record, "keyItems");
            m_selfEvaluation = this.GetStringValue(record, "selfEvaluation");
            m_existingGap = this.GetStringValue(record, "existingGap");
            m_planFinishDate = this.GetDateValue(record, "planFinishDate");
            m_actualFinishDate = this.GetDateValue(record, "actualFinishDate");
            m_status = (FavoriteCodificationStatus)this.GetIntValue(record, "status");
            return true;
        }

        protected string GetConditionForEnterpriseAndCodification(int enterpriseID, int docID)
        {
            return string.Format("where {0}={1} and {2}={3}",
                DB.GetCol("enterpriseID"), DB.GetVal(enterpriseID),
                DB.GetCol("docID"), DB.GetVal(docID));
        }

        public bool Exist(int enterpriseID, int docID)
        {
            return this.Exist(null, this.GetConditionForEnterpriseAndCodification(enterpriseID, docID), true);
        }
        public R_EnterpriseCodification GetPropertiesByEnterpriseAndCodification(int enterpriseID, int docID)
        {
            var res = new R_EnterpriseCodification();
            var list = GetListByEnterpriseAndCodification(enterpriseID, docID);
            if (list.Count > 0)
            {
                res = list[0];
            }
            return res;
        }
        public R_EnterpriseCodification GetEnterpriseCodificationByID(int id)
        {
            R_EnterpriseCodification e_cod = null;
            List<R_EnterpriseCodification> e_cods = this.GetSpecialList<R_EnterpriseCodification>(null, "id", id);
            if (e_cods.Count > 0)
            {
                e_cod = e_cods[0];
            }

            return e_cod;
        }
        public List<R_EnterpriseCodification> GetListByEnterpriseAndCodification(int enterpriseID, int docID)
        {
            return this.GetSpecialList<R_EnterpriseCodification>(null, this.GetConditionForEnterpriseAndCodification(enterpriseID, docID));
        }
        public List<R_EnterpriseCodification> GetListByCodification(int docID)
        {
            return this.GetSpecialList<R_EnterpriseCodification>(null, "docID", docID);
        }
        public List<R_EnterpriseCodification> GetListByEnterprise(int enterpriseID)
        {
            return this.GetSpecialList<R_EnterpriseCodification>(null, "enterpriseID", enterpriseID);
        }
        public List<R_EnterpriseCodification> GetList(int enterpriseID, int catalogID)
        {
            List<R_EnterpriseCodification> result = new List<R_EnterpriseCodification>();
            List<R_EnterpriseCodification> all = this.GetSpecialList<R_EnterpriseCodification>(null, "enterpriseID", enterpriseID);

            foreach (R_EnterpriseCodification cod in all)
            {
                if (cod.Codification != null && cod.Codification.CatalogID == catalogID)
                {
                    result.Add(cod);
                }
            }

            return result;
        }

        public void DeleteByCodification(string transID, int docID)
        {
            List<R_EnterpriseCodification> list = new R_EnterpriseCodification().GetListByCodification(docID);
            for (int i = 0; i < list.Count; i++)
            {
                list[i].Delete(transID);
            }
        }
        public void DeleteByEnterprise(string transID, int enterpriseID)
        {
            List<R_EnterpriseCodification> ecList = new R_EnterpriseCodification().GetListByEnterprise(enterpriseID);
            for (int i = 0; i < ecList.Count; i++)
            {
                ecList[i].Delete(transID);
            }
        }

        public override int Delete(string transID)
        {
            new R_CodificationUser().DeleteByCodification(transID, m_id);
            new Plan().DeleteByCodification(transID, m_id);
            return base.Delete(transID);
        }

        public DataTable GetReportForPrincipal(int enterpriseID)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("principal");
            dt.Columns.Add("number");

            IDictionary<string, int> dic = new Dictionary<string, int>();

            IList<R_EnterpriseCodification> e_cod_list = GetListByEnterprise(enterpriseID);
            foreach (R_EnterpriseCodification e_cod in e_cod_list)
            {
                if (!string.IsNullOrEmpty(e_cod.Principal))
                {
                    if (!dic.ContainsKey(e_cod.Principal))
                    {
                        dic.Add(e_cod.Principal, 1);
                    }
                    else
                    {
                        dic[e_cod.Principal] = dic[e_cod.Principal] + 1;
                    }
                }
            }

            foreach (string key in dic.Keys)
            {
                dt.Rows.Add(key, dic[key]);
            }

            return dt;
        }

        public List<R_EnterpriseCodification> GetReportForPriority(int enterpriseID, string cond)
        {
            string sql = string.Format("select t1.* from t_codification t, t_r_enterprise_codification t1 where t.id=t1.docID and {0}={1} and {2}",
                "t1.enterpriseID", DB.GetVal(enterpriseID), cond);
            System.Collections.Generic.List<R_EnterpriseCodification> res = new System.Collections.Generic.List<R_EnterpriseCodification>();
            DataTable dt = DB.Select(sql).Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                R_EnterpriseCodification ett = new R_EnterpriseCodification();
                ett.GetProperties(dt.Rows[i]);
                res.Add(ett);
            }
            return res;
        }
    }
}
